package net.bplaced.streetfighter.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.bplaced.streetfighter.MatchDetailActivity;
import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.model.Match;
import net.bplaced.streetfighter.lib.utils.RuntimeData;
import net.bplaced.streetfighter.lib.utils.RuntimeDataKey;
import net.bplaced.streetfighter.lib.utils.RuntimeDataObject;
import net.bplaced.streetfighter.lib.utils.StreetfighterUtils;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class MatchAdapter
        extends BaseAdapter
        implements OnClickListener {

    private final Context context;
    private final List<Match> matches;

    public MatchAdapter(final Context context, final List<Match> matches) {

        this.context = context;
        this.matches = matches;
    }

    public void add(Match match) {

        this.matches.add(match);
        this.notifyDataSetChanged();
    }

    public void addAll(List<Match> matches) {

        this.matches.addAll(matches);
        this.notifyDataSetChanged();
    }

    public void addAll(Match[] matches) {

        for (final Match m : matches) {
            this.matches.add(m);
        }
        this.notifyDataSetChanged();
    }

    public void clear() {

        this.matches.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return this.matches.size();
    }

    @Override
    public Match getItem(int position) {

        return this.matches.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Match m = this.matches.get(position);

        if (convertView == null) {
            final LayoutInflater inflater =
                    (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.match_adapter_item, null);
        }

        // Opponent field
        final TextView oppField = (TextView) convertView.findViewById(R.id.match_adapter_item_opponent);
        oppField.setText(m.opponent);

        final Date tempDateTime = StreetfighterUtils.convertToDate(m.matchDate, m.matchTime);

        // Date
        final TextView dateField = (TextView) convertView.findViewById(R.id.match_adapter_item_date);
        dateField.setText(new SimpleDateFormat("dd.MM.yyyy").format(tempDateTime));

        // Time
        final TextView timeField = (TextView) convertView.findViewById(R.id.match_adapter_item_time);
        timeField.setText(new SimpleDateFormat("HH:mm").format(tempDateTime));

        // Category field
        final TextView categoryField = (TextView) convertView.findViewById(R.id.match_adapter_item_category);

        if (m.matchCategory == 2) {
            categoryField.setText(R.string.match_heim);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_heim));
        } else if (m.matchCategory == 3) {
            categoryField.setText(R.string.match_auswaerts);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_auswaerts));
        } else if (m.matchCategory == 4) {
            categoryField.setText(R.string.match_pokal);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_pokal));
        } else if (m.matchCategory == 5) {
            categoryField.setText(R.string.match_playoff);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_playoff));
        } else if (m.matchCategory == 6) {
            categoryField.setText(R.string.match_testspiel);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_testspiel));
        }

        convertView.setTag(m);
        convertView.setOnClickListener(this);

        return convertView;
    }

    @Override
    public void onClick(View v) {

        final Match m = (Match) v.getTag();
        final Intent matchDetailIntent = new Intent(this.context, MatchDetailActivity.class);
// matchDetailIntent.putExtra(Match.INTENTKEY_MATCH, m);
        RuntimeData.getInstance().put(RuntimeDataKey.MATCH, new RuntimeDataObject<Match>(m));
        this.context.startActivity(matchDetailIntent);
    }
}